import { defineStore } from 'pinia'
import { reqLogin, reqUserInfo } from "@/api/user";
import { ref } from 'vue'
import type { loginFormData, loginResponseData, userInfoResponseData } from '@/api/user/type'
import { anyRoute, asyncRoute, constantRoute } from '@/router/routes'
import router from '@/router'

//引入深拷贝方法
//@ts-ignore
import cloneDeep from 'lodash/cloneDeep'

//用于过滤当前用户需要展示的异步路由
function filterAsyncRoute(asyncRoute: any, routes: any) {
    return asyncRoute.filter((item: any) => {
        if (routes.includes(item.name)) {
            if (item.children && item.children.length > 0) {
                item.children = filterAsyncRoute(item.children, routes)
            }
            return true
        }
    })
}

export const useUserSrote = defineStore('User', {
    state: () => {
        return {
            username:ref(''),
            avatar:ref(''),
            account: ref(''),
            password: ref(''),
            token: ref(localStorage.getItem('TOKEN')),
            routeList: ref(constantRoute),
            userInfo:ref(localStorage.getItem('USERINFO')),
            recordRouter: ref(localStorage.getItem('RECORDROUTER'))
        }
    },
    actions: {
        // 用户登录
        async userLogin(data: loginFormData) {
            let res = await reqLogin(data)
            console.log(res)
            if (res.code == 1) {
                this.token = res.data.token
                localStorage.setItem('TOKEN', res.data.token)
            }
            return res
        },

        // 用户信息
        async getUserInfo() {
            let res = await reqUserInfo()
            if (res.code == 1) {
                
                let useInfo = res.data
                this.username = useInfo.user_name
                this.avatar = useInfo.avatar
                useInfo.arrRule = []
                useInfo.rule.map(item=>{
                    useInfo.arrRule.push(item.name)
                })
                useInfo.arrRule.push('Bond')

                //
                // let bond = ['BondPay','BondPaySee','Bond','BondRefund']
                    //计算当前用户需要展示的异步路由(深拷贝)
                    const userAsyncRoute = filterAsyncRoute(cloneDeep(asyncRoute), useInfo.arrRule)
                    console.log(userAsyncRoute)
                    this.routeList = [...constantRoute, ...userAsyncRoute, ...anyRoute]
                    //目前路由器管理的只有常量路由:用户计算完毕异步路由、任意路由动态追加
                    const userAllRoute = [...userAsyncRoute, ...anyRoute]
                    console.log(userAsyncRoute)
                    userAllRoute.forEach((route: any) => {
                        router.addRoute(route)
                    })
            } else {
                return Promise.reject(new Error(res.data))
            }
        },

        // 退出登录
        logout() {
            this.token = ''
            localStorage.removeItem('TOKEN')
            localStorage.removeItem('RECORDROUTER')
            localStorage.removeItem('USERINFO')
        }
    },
    getters: {}
})